package black.market.weather;

import java.sql.Date;
import java.sql.Time;

import android.app.ExpandableListActivity;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.TextView;
import black.market.location.LocationDAO;
import black.market.location.MapContentProvider;
import black.market.location.ReadLocation;
import black.market.main.BlackPublic.Action;
import black.market.main.R;
import black.market.util.Log;
import black.market.util.Util;

public class WeatherInfoActivity extends ExpandableListActivity {
	public static final String TAG = "WeatherInfoActivity";
	private Context mContext = null;
	private ContentResolver mContentResolver = null;
	private WeatherExpandableListAdapter expandableListAdapter = null;
	private KmaInfoDAO kmaInfoDAO = null;
	private KmaInfoData kmaInfoData = null;
	private int[] kmaTimeMap = { 2, 5, 8, 11, 14, 17, 20, 23 };
	private ReadLocation readLocation = null;
	private TextView customTitle = null;
	BroadcastReceiver receiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			Log.d(TAG, "intent action: " + intent.getAction());
			// setTitleLabel();
			switch (Action.toAction(intent.getAction())) {
			case GET_KMA_DATA:
				if (expandableListAdapter == null) {
					expandableListAdapter = new WeatherExpandableListAdapter(mContext, kmaInfoDAO);
					setListAdapter(expandableListAdapter);
				} else {
					expandableListAdapter.notifyDataSetChanged();
				}

				break;
			default:
				break;
			}
		}
	};

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		customTitle = (TextView) findViewById(R.id.left_text);
		mContext = getApplicationContext();
		mContentResolver = getContentResolver();
		kmaInfoDAO = new KmaInfoDAO(mContext);
		readLocation = new ReadLocation(mContext);
		kmaInfoData = new KmaInfoData(mContext);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		if (readLocation.isbServiceState()) {
			readLocation.stopReadLocation();
		}
		super.onPause();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		boolean isBodyTable = kmaInfoDAO.isKmaDatabase_Null(WeatherContentProvider.BODY_URI);
		if (Util.isUriDatabaseCheck(mContentResolver, WeatherContentProvider.BODY_URI)) {
			if (Util.isUriDatabaseCheck(mContentResolver, MapContentProvider.MAP_ADDRESS_URI)) {
				Log.d(TAG, "isUriDatabaseCheck(MapContentProvider.MAP_ADDRESS_URI: " + Util.isUriDatabaseCheck(mContentResolver, MapContentProvider.MAP_ADDRESS_URI));
				Intent intent = new Intent();
				intent.setAction(Action.LOCATION_CHANGED_ACTION.toString());
				sendBroadcast(intent);
			} else {
				Log.d(TAG, "WeatherInfoActivity isBodyTable: " + isBodyTable);
				Intent intent = new Intent();
				intent.setAction(Action.KMA_DATA_INTERVAL3H.toString());
				sendBroadcast(intent);
			}
		} else {
			Log.i(TAG, "onResume: weather info ok");
			expandableListAdapter = new WeatherExpandableListAdapter(mContext, kmaInfoDAO);
			setListAdapter(expandableListAdapter);
		}
	}

	public void createCheck() {
		SharedPreferences preferences = getSharedPreferences(Action.SHARED_PREF.toString(), MODE_WORLD_READABLE);
		boolean isLoc = preferences.getBoolean(Action.READLOCATION_1H.toString(), false);
		boolean isKma = preferences.getBoolean(Action.KMA_DATA_INTERVAL3H.toString(), false);
		if (isLoc) {

		} else {
			Intent intent = new Intent();
			intent.setAction(Action.KMA_DATA_INTERVAL3H.toString());
			sendBroadcast(intent);
		}
	}

	public boolean isSharedPref() {
		SharedPreferences preferences = getSharedPreferences(Action.SHARED_PREF.toString(), MODE_WORLD_READABLE);
		boolean isLoc = preferences.getBoolean(Action.READLOCATION_1H.toString(), false);
		boolean isKma = preferences.getBoolean(Action.KMA_DATA_INTERVAL3H.toString(), false);
		return (isKma || isLoc);
	}

	public void setTitleLabel() {
		LocationDAO locationDAO = new LocationDAO(mContext);
		String[] locArr = locationDAO.getSimpleAddr();
		String str = kmaInfoDAO.getKmaHeaderLastTimeMap();
		if (locArr[0] != null && str != null) {
			customTitle.setText(str.substring(0, 4) + "년 " + str.substring(4, 6) + "월 " + str.substring(6, 8) + "일 " + str.substring(8, 10) + "시  자료제공처: 기상청" + "\n" + locArr[0] + " " + locArr[1]
					+ " " + locArr[2]);
		}
	}

	public boolean isCheckedLostTimeMap() {
		Time t = new Time(System.currentTimeMillis());
		Date d = new Date(System.currentTimeMillis());
		String lastTm = kmaInfoDAO.getKmaHeaderLastTimeMap();
		int[] tmArr = { Integer.parseInt(lastTm.substring(0, 4)), Integer.parseInt(lastTm.substring(4, 6)), Integer.parseInt(lastTm.substring(6, 8)), Integer.parseInt(lastTm.substring(8, 10)) };
		boolean[] isTmArr = { d.getMonth() > tmArr[1], d.getDate() > tmArr[2], t.getHours() > tmArr[3] };
		boolean isDateChange = false;
		for (int i = 0; i < kmaTimeMap.length; i++) {
			if (0 < kmaTimeMap[i]) {
				Log.d(TAG, "isCheckedLostTimeMap()1: " + i + "= " + isTmArr[1]);
				isDateChange = isTmArr[1];
				if (isDateChange) {
					break;
				}
			}
			if (kmaTimeMap[i] < t.getHours()) {
				Log.d(TAG, "isCheckedLostTimeMap()2: " + i + "= " + isTmArr[2]);
				isDateChange = isTmArr[2];
				if (isDateChange) {
					break;
				}
			}
		}
		return isDateChange;
	}
}